package common;

import org.apache.rocketmq.shaded.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class QWModelRegistry {
    private static final Logger log = LoggerFactory.getLogger(QWModel.class);
    private static boolean initialized = false;
    private static QWModel qwModel;

    public static synchronized void registerQWenModel(String apiKey) {
        if (StringUtils.isEmpty(apiKey)) {
            System.out.println("QWen 模型初始化过程中的apiKey为空，请进行配置百炼平台QWen模型的apiKey");
            return;
        }
        if (initialized) {
            log.debug("QWen 模型已经初始化过了");
            return;
        }
        try {
            qwModel = new QWModel(apiKey);
            initialized = true;
            log.info("✅ QWen 模型初始化成功");
        } catch (Exception e) {
            log.error("❌ QWen 模型初始化失败: {}", e.getMessage(), e);
            throw new RuntimeException("Failed to initialize QWen model", e);
        }
    }

    public static boolean isRegistered() {
        return initialized;
    }

    public static QWModel getModel(String apiKey) {
        if (!initialized) {
            registerQWenModel(apiKey);
        }
        return qwModel;
    }
}
